<template>
	<view>
			<view class="list b-b" v-if="merchantInfo && merchantInfo.merchantName">
				<view class="wrapper">
					<view class="merchant-box">
						<text class="merchant">{{merchantInfo.merchantName}}</text>
					</view>
					<view class="u-box">
						<text class="name">{{merchantInfo.merchantAddress}}</text>
					</view>
				</view>
				<view class="distance">
					<view class="button-base-outline" @click="changeMerchant(merchantInfo)">切换</view>
				</view>
			</view>
			<view class="list b-b" v-else>
				<icon type="warn" size="20"/>
				<view class="no-merchant">
					你还没有设置提货点
				</view>
				<view class="distance">
					<view class="button-base-outline" @click="changeMerchant(merchantInfo)">马上设置</view>
				</view>
			</view>
			

		<view class="row b-b">
			<text class="tit">提货人</text>
			<input class="input" :maxlength="10" type="text" v-model="deliveryName" placeholder="提货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号码</text>
			<input class="input" :maxlength="11" type="text" v-model="deliveryContactNo" placeholder="提货人手机号码" placeholder-class="placeholder" />
		</view>
		<view class="goods-section">

			<!-- 商品列表 -->
			<view v-for="cart in carts">
				<view class="g-item">
					<image :src="cart.productDTO.productMainImage.url"></image>
					<view class="right">
						<text class="title clamp">{{cart.productDTO.productName}}</text>
						<text class="spec" v-if="cart.productDTO.skuEnabled">
							<text v-for="sku in cart.productSkuDTO.skuAttrValueList">{{sku.skuAttrValue}}</text>
						</text>
						<text class="spec" v-if="!cart.productDTO.skuEnabled">-</text>
						<view class="price-box">
							<text class="price">￥{{cart.unitPrice}}</text>
							<text class="number">x {{cart.unit}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 优惠明细, 如果商品包含多个商家则不能使用优惠券 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<!-- <view class="yt-list-cell b-b" @click="toggleMask('show')"> -->
				<view class="cell-icon">
					券
				</view>
				<text class="cell-tit clamp">优惠券</text>
				<text class="cell-tip" v-if="coupons.length==0">
					<text>无可用优惠券</text>
				</text>
				<text class="cell-tip active" v-if="coupons.length>0" @click="togglePopup('bottom', 'coupon')">
					<text v-if="!selectedCoupon.userCouponUuid">{{coupons.length}}张优惠券可用</text>
					<text class="coupon-name" v-if="selectedCoupon.userCouponUuid">
						{{selectedCoupon.couponDTO.name}}
					</text>
					<text class="deduct-amount" v-if="selectedCoupon.userCouponUuid">
						{{deductAmount}}
					</text>
				</text>
				<uni-icons class="close" type="clear" color="#909399" size="20" @click="clearSelectedCoupon" v-if="selectedCoupon.userCouponUuid"></uni-icons>
			</view>
		</view>
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">商品金额</text>
				<text class="cell-tip red">￥{{productAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">共优惠</text>
				<text class="cell-tip">￥{{deductAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip">￥{{freightAmount}}</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="memo" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{actualAmount>0?actualAmount:0}}</text>
			</view>
			
			<view class="submit">
				<view class="button-base" @click="submit">
					提交订单
				</view>
			</view>
			
		</view>

		<!-- 优惠券面板 -->
		<!-- 底部分享弹窗 -->
		<uni-popup ref="showcoupon" :type="type" @change="change">
			<scroll-view scroll-y="true" class="coupon-list" @touchmove.stop>
				<user-coupon-item :coupon="item" action-text="立即使用" @eventClick="selectCoupon(item)" v-for="item in coupons"></user-coupon-item>
			</scroll-view>
		</uni-popup>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import uniPopup from "@/components/uni-popup/uni-popup.vue"
	import userCouponItem from "@/components/user-coupon-item.vue"
	
	export default {
		data() {
			return {
				deliveryName:'',
				deliveryContactNo:'',
				cartIds: [],
				carts: [], //结算的商品
				productAmount: 0.00, //商品金额, 未扣减任何优惠
				deductAmount: 0.00, //优惠金额
				actualAmount: 0.00, //实付金额
				freightAmount: 0.00, //运费
				maskState: 0, //优惠券面板显示状态
				memo: '', //备注
				payType: 1, //1微信 2支付宝
				coupons: [], //有效优惠券
				selectedCoupon: {}, //选择使用的优惠券
				type: ''
			}
		},
		components: {
			uniPopup,
			userCouponItem
		},
		onLoad(option) {
			//商品数据
			var cart_id = JSON.parse(option.data);
			if (cart_id.length < 0) {
				uni.navigateBack({
					delta: 1
				})
			}
			this.cartIds = cart_id;
			this.deliveryName = this.userInfo.name;
			this.deliveryContactNo = this.userInfo.personalPhone;
			this.inquiryProductByCartId(this.cartIds);
			this.calculateFreightAmount(this.cartIds);
			this.searchCoupon();
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'hasMerchant', 'merchantInfo', 'applicationConfig'])
		},
		watch: {
			
		},
		methods: {
			...mapMutations(['updateCartInfo']),
			changeMerchant(item){
				uni.navigateTo({
					url: '/pages/merchant/merchant'
				})
			},
			//查询订单商品
			inquiryProductByCartId(cardIds) {
				let that = this;
				let postData = cardIds.split(',');
				this.$api.request.productByCardIds(postData, res => {
					if (res.body.status.statusCode === '0') {
						this.carts = res.body.data.carts;
						var productAmount = this.calcSelectedProductAmount(this.carts);
						this.productAmount = productAmount.toFixed(2);
						this.actualAmount = (this.productAmount - this.deductAmount + this.freightAmount).toFixed(2);
						//查询最佳扣减优惠券
						var orderProductDTOList = [];
						this.carts.forEach(function(cart,index){
							orderProductDTOList.push({
								productDTO:{
									productUuid: cart.productDTO.productUuid
								},
								productUnit: cart.unit,
								productUnitPrice: cart.unitPrice
							});
						})
						this.getBestCoupon(orderProductDTOList);
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			//获取扣减最多的优惠券
			getBestCoupon(orderProductDTOList) {
				let postData = {
					userDTO:{
						userUuid:this.userInfo.userUuid
					},
					orderProductDTOList:orderProductDTOList
				};
				this.$api.request.bestCoupon(postData, res => {
					if (res.body.status.statusCode === '0') {
						var selectedCoupon = res.body.data;
						if(selectedCoupon){
							this.selectCoupon(selectedCoupon)
						}
					}
				}, true);
			},
			//搜索可用优惠券
			searchCoupon() {
				var keyArray = ['USER', 'IS_EXPIRED', 'IS_USED', 'IS_STARTED'];
				let postData = {
					keyArray: keyArray,
					userUuid: this.userInfo.userUuid,
					expired: false,
					used: false,
					started: true,
					startIndex: 0,
					pageSize: 50 //默认查询50张有效优惠券, 用户大于50张则多余部分不会显示
				};
				//加载中
				this.$api.request.userCoupon(postData, res => {
					if (res.body.status.statusCode === '0') {
						var coupons = res.body.data.userCoupons;
						this.coupons = coupons;
					}
				}, true);
			},
			// 不使用优惠券
			clearSelectedCoupon() {
				this.selectedCoupon = {};
				this.deductAmount = 0.00;
				this.actualAmount = (this.productAmount - this.deductAmount + this.freightAmount).toFixed(2);
			},
			// 选中优惠券
			selectCoupon(item) {
				//计算当前商品使用优惠券后的优惠金额
				let postData = {
					shoppingCartUuidList: this.cartIds.split(','),
					userCouponDTO: {
						couponDTO: {
							couponUuid: item.couponDTO.couponUuid
						}
					}
				}
				this.$api.request.calculateCouponAmount(postData, res => {
					if (res.body.status.statusCode === '0') {
						this.deductAmount = res.body.data.deductAmount;
						this.actualAmount = (this.productAmount - this.deductAmount + this.freightAmount).toFixed(2);
						this.selectedCoupon = item;
						this.$refs.showcoupon.close();
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				}, false);
			},
			// 计算运费金额
			calculateFreightAmount(item, province, city, area) {
				let postData = {
					shoppingCartUuidList: this.cartIds.split(','),
					merchantDTO: {
						merchantUuid: this.merchantInfo.merchantUuid
					}
				}
				this.$api.request.calculateFreightAmount(postData, res => {
					if (res.body.status.statusCode === '0') {
						this.freightAmount = res.body.data.freightAmount;
						this.actualAmount = (this.productAmount - this.deductAmount + this.freightAmount).toFixed(2);
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				}, false);
			},
			//计算总金额
			calcSelectedProductAmount(cartData) {
				var amount = 0;
				cartData.forEach(function(val, index) {
					amount = amount + val.unitPrice * val.unit;
				})
				return amount;
			},
			change(e) {
				console.log('是否打开:' + e.show)
			},
			togglePopup(type, open) {
				this.type = type
				this.$nextTick(() => {
					this.$refs['show' + open].open()
				})
			},
			//显示优惠券面板
			toggleMask(type) {
				let timer = type === 'show' ? 10 : 300;
				let state = type === 'show' ? 1 : 0;
				this.maskState = 2;
				setTimeout(() => {
					this.maskState = state;
				}, timer)
			},
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type) {
				this.payType = type;
			},
			submit() {
				//检查提货点是否选择
				if (!this.merchantInfo || !this.merchantInfo.merchantName) {
					this.$api.msg('请选择提货点');
					return;
				}
				//检查提货人是否填写
				if (!this.deliveryName) {
					this.$api.msg('请输入提货人姓名');
					return;
				}
				if(!this.$api.util.validateMobileNo(this.deliveryContactNo)){
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				//post订单数据到后台
				let postData = {
					actionType: 'ADD',
					userDTO: {
						userUuid: this.userInfo.userUuid
					},
					deliveryName: this.deliveryName,
					deliveryContactNo: this.deliveryContactNo,
					merchantDTO:{
						merchantUuid: this.merchantInfo.merchantUuid
					},
					memo: this.memo,
					shoppingCartUuidList: this.cartIds.split(',')
				}
				if (this.selectedCoupon.userCouponUuid) {
					postData.userCouponDTO = {
						userCouponUuid: this.selectedCoupon.userCouponUuid,
						couponDTO: {
							couponUuid: this.selectedCoupon.couponDTO.couponUuid
						}
					}
				}
				this.$api.request.createOrder(postData, res => {
					if (res.body.status.statusCode === '0') {
						//同步购物车
						this.syncUserCart(function(){
							uni.redirectTo({
								url: '/pages/money/pay?orderNo=' + res.body.data.orderNo
							})
						});
						
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				});

			},
			//同步购物车数据到本地
			syncUserCart(callback) {
				if(this.hasLogin){
					let postData = {
						userUuid: this.userInfo.userUuid
					}
					this.$api.request.cartList(postData, res => {
						if (res.body.status.statusCode === '0') {
							let cartList = res.body.data.carts;
							this.updateCartInfo(cartList);
							if(callback) callback();
						} else {
							console.log(res.body.status.errorDesc);
						}
					},false);
				}
			},
			stopPrevent() {}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}
	.m-list{
		margin: 20upx 0;
	}
	.no-merchant{
		.address{
			text-align: center;
			padding: 40upx;
			color: $font-color-dark;
			font-size: $font-lg+4upx;
		}
		.desc{
			text-align: center;
			margin: 40upx;
			color: $font-color-base;
		}
	}
	.list {
		display: flex;
		align-items: center;
		padding: 20upx 30upx;
		margin: 20upx 0;
		background: #fff;
		position: relative;
		.distance {
			width: 200upx;
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-left: 20px;
			justify-content: right;
			color: $font-color-light;
			font-size: $font-base;
			text-align: right;
			.button-base-outline{
				width: 160upx;
			}
		}
		.no-merchant{
			margin-right: auto;
			font-size: $font-lg;
			color: $font-color-dark;
		}
	}
	.merchant-box {
		display: flex;
		align-items: center;
	
		.tag {
			font-size: 24upx;
			color: $base-color;
			margin-right: 10upx;
			background: $page-color-light;
			border: 1px solid $border-color-dark;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
	
		.merchant {
			font-size: $font-lg;
			color: $font-color-dark;
		}
	}
	.u-box {
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;
	
		.name {
			margin-right: 30upx;
		}
	}
	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
		
		.tit{
			flex-shrink: 0;
			width: 160upx;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}
	}

	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}

				.number {
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: $page-color-light;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: $base-color;
			border-radius: 4upx;
			margin-right: 12upx;

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-light;
			.coupon-name{
				background: $uni-color-primary;
				color:#fff;
				padding: 6upx;
			}
			.deduct-amount{
				margin-left:20upx;
				&::before{
					content: '-￥';
				}
			}

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $font-color-dark;
			}

			&.red {
				color: $base-color;
			}

			& .close {
				color: $font-color-light;
				margin-left: 20upx;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	/* 支付列表 */
	.pay-list {
		padding-left: 40upx;
		margin-top: 16upx;
		background: #fff;

		.pay-item {
			display: flex;
			align-items: center;
			padding-right: 20upx;
			line-height: 1;
			height: 110upx;
			position: relative;
		}

		.tit {
			font-size: 32upx;
			color: $font-color-dark;
			flex: 1;
		}
	}

	.footer {
		position: fixed;
		bottom: var(--window-bottom);
		display: flex;
		align-items: center;
		padding: 0 30upx;
		width: 100%;
		height: 140upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, .1);
	
		.price-content {
			padding-left: 30upx;
		}
	
		.price-tip {
			color: $base-color;
			margin-left: 8upx;
		}
	
		.price {
			font-size: 36upx;
			color: $base-color;
		}
	
		.submit {
			align-items: center;
			justify-content: center;
			width: 280upx;
		}
	}

	/* 优惠券面板 */
	.mask {
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: var(--window-bottom);
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 9995;
		transition: .3s;

		.mask-content {
			width: 100%;
			min-height: 30vh;
			max-height: 70vh;
			background: $page-color-light;
			transform: translateY(100%);
			transition: .3s;
			overflow-y: scroll;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.mask-content {
				transform: translateY(0);
			}
		}
	}

	/* 优惠券列表 */
	.coupon-item {
		display: flex;
		flex-direction: column;
		margin: 20upx 24upx;
		background: #fff;

		.con {
			display: flex;
			align-items: center;
			position: relative;
			height: 120upx;
			padding: 0 30upx;

			&:after {
				position: absolute;
				left: 0;
				bottom: 0;
				content: '';
				width: 100%;
				height: 0;
				border-bottom: 1px dashed #f3f3f3;
				transform: scaleY(50%);
			}
		}

		.left {
			display: flex;
			flex-direction: column;
			justify-content: center;
			flex: 1;
			overflow: hidden;
			height: 100upx;
		}

		.title {
			font-size: 32upx;
			color: $font-color-dark;
			margin-bottom: 10upx;
		}

		.time {
			font-size: 24upx;
			color: $font-color-light;
		}

		.right {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 26upx;
			color: $font-color-base;
			height: 100upx;
		}

		.price {
			font-size: 44upx;
			color: $base-color;

			&:before {
				content: '￥';
				font-size: 34upx;
			}
		}

		.tips {
			font-size: 24upx;
			color: $font-color-light;
			line-height: 60upx;
			padding-left: 30upx;
		}

		.circle {
			position: absolute;
			left: -6upx;
			bottom: -10upx;
			z-index: 10;
			width: 20upx;
			height: 20upx;
			background: $page-color-light;
			border-radius: 100px;

			&.r {
				left: auto;
				right: -6upx;
			}
		}
	}

	.coupon-list {
		height: 400px;
		display: flex;
		flex-wrap: wrap;
		// padding: 0 30upx;
		border-radius: 20upx;
		margin-bottom: 90upx;

		.coupon-list_item {
			width: 100%;
			height: 110px;
			margin: 10upx 0;
			background-color: #fff;
			border-radius: 5px;

			.coupon-left {
				float: left;
				width: 45%;
				height: 100%;
				padding: 10upx;

				image {
					width: 100%;
					height: 100%;
					opacity: 1;
				}
			}

			.coupon-right {
				position: relative;
				padding: 10upx;
				width: 55%;
				float: left;
				border-radius: 0px 10px 10px 0px;

				.name {
					color: $font-color-dark;
					font-size: $font-lg;
				}

				.valid {
					color: $font-color-light;
					font-size: $font-base;
					line-height: 40upx;
				}

				.stock {
					color: $font-color-light;
					font-size: $font-base;
					line-height: 40upx;
					margin-top: 10upx;
				}

				image {
					position: absolute;
					top: 10upx;
					right: 10upx;
					height: 100upx;
					width: 100upx;
				}
			}
		}
	}
	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, .06);
		/* position: relative; */
		z-index: 10;
	
		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;
			position: relative;
	
			&.current {
				color: $base-color;
	
				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}
	}
</style>
